﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using NUnit.Framework;

using Refix.Core.Commands;
using Refix.Core.Tests.Commands;

namespace Refix.Core.Tests.Options
{
    [TestFixture]
    public class UpgradeTest : TestSolutionFolderTest
    {
        [Test]
        public void Upgrade_Does_Nothing_If_No_Compatible_Set_Found()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Verbose;

            var queue = new Queue<string>();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            Command clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.2+");
            Command alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("res");
            queue.Enqueue("-u");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("Refix.TestSolution.sln");
            Command resolve = new CommandWithOptions(queue).Command;
            resolve.Validate();

            Assert.True(resolve.Execute());
            string progressSoFar = builder.ToString();
            Assert.True(progressSoFar.Contains("Attempting to upgrade"));
            Assert.False(progressSoFar.Contains("Upgraded"));

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("-d");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.2+");
            alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            Console.Write(builder.ToString());
        }

        [Test]
        public void Upgrade_Succeeds_If_CompatibleVersion_Found()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Verbose;

            var queue = new Queue<string>();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            Command clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.3");
            Command alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.3");
            queue.Enqueue("0.1.0.4");
            alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("res");
            queue.Enqueue("-u");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("Refix.TestSolution.sln");
            Command resolve = new CommandWithOptions(queue).Command;
            resolve.Validate();

            Assert.True(resolve.Execute());
            string progressSoFar = builder.ToString();
            Assert.True(progressSoFar.Contains("Attempting to upgrade"));
            Assert.True(progressSoFar.Contains("Upgraded Refix.Test.A version from 0.1.0.3 to 0.1.0.4"));

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("-d");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.3");
            alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("-d");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.3");
            queue.Enqueue("0.1.0.4");
            alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            Console.Write(builder.ToString());
        }
    }
}